home *** CD-ROM | disk | FTP | other *** search
- Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
- From: grantp@usa.pipeline.com(Pete Grant)
- Newsgroups: comp.lang.c++
- Subject: Re: Need help using g++ and Templates
- Date: 25 Mar 1996 23:04:24 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4j78po$foa@news1.h1.usa.pipeline.com>
- References: <4j7017$8m4@sal-sun133.usc.edu>
- NNTP-Posting-Host: 38.8.60.8
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete Grant)
- X-Newsreader: Pipeline v3.5.0
-
- On Mar 25, 1996 12:34:47 in article <Need help using g++ and Templates>,
- 'radford@sal-sun133.usc.edu (Richard Radford)' wrote:
-
-
- >I have created object files for the template class and the files that use
-
- >instances of the template class. When I compile using g++ version 2.7 I
- get
- >errors saying I have a bunch of undefined symbols. The template i'm trying
- to
- >use is for lists. All the undefined symbols are of the form List<Class I'm
-
- >trying to create a list of>::member_function_of_template. Can anyone offer
- any
- >advice on how to compile templates using g++. Any help would be greatly
- >appreciated. Please respond by email. I have never posted to a news group
-
- >before and I'm not sure how I would retrieve any advice you may have for
- me.
- >
- > Thanks in advance, Richard Radford radford@nunki.usc.edu
-
- There are other ways, but the easiest is to place all your template
- definitions into header file(s) and include the header(s) in all your
- source modules that use those templates. Something like:
-
- ////// file foo.h
-
- template<class T>
- class Stack
- {
- public:
- Stack(int size = 0);
- T& pop();
- void push(T&);
- ....
- };
-
- template <class T>
- Stack<T>::Stack(int siz) { ... body }
-
- template<class T>
- T& Stack<T>::pop { return whatever...}
-
- I recommend that all template members for a class be placed
- in a single header file. Also, it might help maintenance
- if each template class is in its own header.
-
- --
- Pete Grant
- Kalevi, Inc.
- Software Engineering & development
-